﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebChatSerivce.Models;
using WebChatService.Models;

namespace WebChatSerivce.Controllers
{
    public class ChatterMessageAPIController : ApiController
    {
        //private WebChatEntities db = new WebChatEntities();
        private IDatabase db;

        public ChatterMessageAPIController()
        {
            db = new ChatterMessageEFDB();
        }

        // GET api/ChatterMessageAPI
        public IEnumerable<ChatterMessage> GetChatterMessages()
        {
            return (IEnumerable<ChatterMessage>)db.GetAllObjects();
        }

        // GET api/ChatterMessageAPI/5
        public ChatterMessage GetChatterMessage(int id)
        {
            ChatterMessage chattermessage = (ChatterMessage)db.GetSingleObject(id);
            if (chattermessage == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return chattermessage;
        }

        // PUT api/ChatterMessageAPI/5
        public HttpResponseMessage PutChatterMessage(int id, ChatterMessage chattermessage)
        {
            if (ModelState.IsValid && id == chattermessage.ID)
            {
                db.UpdateObject(chattermessage);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/ChatterMessageAPI
        public HttpResponseMessage PostChatterMessage(ChatterMessage chattermessage)
        {
            if (ModelState.IsValid)
            {
                db.AddObject(chattermessage);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, chattermessage);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = chattermessage.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/ChatterMessageAPI/5
        public HttpResponseMessage DeleteChatterMessage(int id)
        {
            ChatterMessage chattermessage = (ChatterMessage)db.GetSingleObject(id);
            if (chattermessage == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.RemoveObject(chattermessage);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, chattermessage);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}